/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cc.altius.web.controller;

import cc.altius.domain.User;
import cc.altius.service.UserService;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 *
 * @author Akil Mahimwala
 */
@Controller
//@RequestMapping("/admin/userList.htm")
public class ListUserController {

    @Autowired
    private UserService userService;

    @ModelAttribute("userList")
    public List<User> populateUserList(HttpSession session) {
        return this.userService.getUserList();
    }

    @RequestMapping(value = "/admin/userList.htm", method = RequestMethod.GET)
    public String showUserListPage() {
        return "/admin/userList";
    }
    
    @RequestMapping(value = "/admin/resetFailedAttempts.htm", method = RequestMethod.GET)
    public String resetFailedAttempts(@RequestParam("userId") int userId,ModelMap map) {
        try {
            User user = new User();
            user.setUserId(userId);
            this.userService.resetFailedCountForUserId(userId);
            
            return "redirect:userList.htm?msg=User updated successfully";
        } catch (Exception e) {
            e.printStackTrace();
            
            return "redirect:userList.htm?msg=failed to reset failed attempts";
        }
    }
}
